package com.ym.b2b.user.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ym.b2b.commons.conf.AgentToken;
import com.ym.b2b.commons.conf.B2BConstants;
import com.ym.b2b.commons.conf.WebProviderTokenUser;
import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.user.service.AgentUserService;
import com.ym.b2b.user.sqlbean.AgentBuyer;
import com.ym.b2b.user.sqlbean.AgentProvider;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Controller
@RequestMapping("/agentuser")
public class AgentUserController extends BaseController {

    private Logger           logger = Logger.getLogger(getClass());
    @Autowired
    private AgentUserService agentUserService;

    @RequestMapping("doAgentBuyer.s")
    public ModelAndView doAgentBuyer(String code, Page<AgentBuyer> page, HttpServletRequest req) {
        ModelAndView m = new ModelAndView();

        AgentToken agentToken = getAgentToken(req);

        Result result = agentUserService.doAgentBuyer(code, agentToken);
        WebTokenUser webTokenUser = (WebTokenUser) result.getData("user");
        webTokenUser.setAgentFlag(true);
        webTokenUser.setAgentToken(agentToken);

        req.getSession().removeAttribute(B2BConstants.WEB_AGENT);
        req.getSession().setAttribute(B2BConstants.WEB_USER_TOKEN, webTokenUser);

        // 取消代理例子 如下 ：去除agent 移除usertoken
        // AgentToken agentToken2 =
        // ((WebTokenUser)req.getSession().getAttribute(B2BConstants.WEB_USER_TOKEN)).getAgentToken();
        // req.getSession().removeAttribute(B2BConstants.WEB_USER_TOKEN);
        // req.getSession().setAttribute(B2BConstants.WEB_AGENT, webTokenUser);

        m.setViewName("center/c_user_center");// /会员中心首页
        return m;
    }

    @RequestMapping("doAgentProvider.s")
    public ModelAndView doAgentProvider(String code, Page<AgentProvider> page, HttpServletRequest req) {
        ModelAndView m = new ModelAndView();

        AgentToken agentToken = getAgentToken(req);

        Result result = agentUserService.doAgentProvider(code, agentToken);
        WebProviderTokenUser webProviderTokenUser = (WebProviderTokenUser) result.getData("providerUser");
        webProviderTokenUser.setAgentFlag(true);
        webProviderTokenUser.setAgentToken(agentToken);

        req.getSession().removeAttribute(B2BConstants.WEB_AGENT);
        req.getSession().setAttribute(B2BConstants.WEB_PROVIDER_TOKEN, webProviderTokenUser);

        m.setViewName("center/c_user_center");// /会员中心首页
        return m;
    }

    /**
     * 代理客户分页列表
     * 
     * @Title: searchPageAgentBuyer
     * @Throws
     */

    @RequestMapping("searchPageAgentBuyer.s")
    @ResponseBody
    public Result searchPageAgentBuyer(AgentBuyer agentBuyer, Page<AgentBuyer> page, HttpServletRequest req) {
        Result result = new Result();
        try {
            AgentToken agentToken = getAgentToken(req);
            if (agentToken != null) {
                agentBuyer.setProviderCode(agentToken.getProviderCode());
                agentBuyer.setIsAgentProvider(agentToken.getIsAgentProvider());
                agentBuyer.setAddBuyer(agentToken.getAddBuyer());
                agentBuyer.setSalesmanId(agentToken.getSalesmanId());
            }
            page = agentUserService.searchPageAgentBuyerList(agentBuyer, page);
            result.setResultPage(page);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * 代理客户分页列表
     * 
     * @Title: searchPageAgentBuyer
     * @Throws
     */

    @RequestMapping("searchPageAgentProvider.s")
    @ResponseBody
    public Result searchPageAgentProvider(AgentProvider agentProvider, Page<AgentProvider> page, HttpServletRequest req) {
        Result result = new Result();
        try {
            AgentToken agentToken = getAgentToken(req);
            if (agentToken != null) {
                agentProvider.setIsAgentProvider(agentToken.getIsAgentProvider());
                agentProvider.setSalesmanId(agentToken.getSalesmanId());
            }
            page = agentUserService.searchPageAgentProviderList(agentProvider, page);
            result.setResultPage(page);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * 取消代理
     */
    @RequestMapping(value = "cancelAgent.s")
    public ModelAndView cancelAgent(HttpServletRequest req) {
        ModelAndView m = new ModelAndView();
        // WebTokenUser webTokenUser = getWebTokenUser(req);
        AgentToken agentToken = new AgentToken();
        Object o1 = req.getSession().getAttribute(B2BConstants.WEB_USER_TOKEN);
        if (null != o1) {
            agentToken = ((WebTokenUser) req.getSession().getAttribute(B2BConstants.WEB_USER_TOKEN)).getAgentToken();
            req.getSession().removeAttribute(B2BConstants.WEB_USER_TOKEN);
        }
        Object o2 = req.getSession().getAttribute(B2BConstants.WEB_PROVIDER_TOKEN);
        if (null != o2) {
            agentToken = ((WebProviderTokenUser) req.getSession().getAttribute(B2BConstants.WEB_PROVIDER_TOKEN)).getAgentToken();
            req.getSession().removeAttribute(B2BConstants.WEB_PROVIDER_TOKEN);
        }
        req.getSession().setAttribute(B2BConstants.WEB_AGENT, agentToken);
        m.setViewName("center/c_user_center");// /会员中心首页
        return m;
    }
}
